package com.example.paopao.passport.controller;

import com.example.paopao.common.pojo.security.CurrenPrincipal;
import com.example.paopao.common.web.JsonResult;
import com.example.paopao.passport.pojo.dto.UserLoginDTO;
import com.example.paopao.passport.pojo.vo.UserLoginResultVO;
import com.example.paopao.passport.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/v1/users/")
@Validated
@Api(tags = "1.单点登录")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 100)
    public JsonResult login(@Valid UserLoginDTO userLoginDTO,@ApiIgnore HttpServletRequest request){
        String remoteAddr = request.getRemoteAddr();
        String header = request.getHeader("User-Agent");
        UserLoginResultVO loginResultVO = userService.login(userLoginDTO, remoteAddr, header);
        System.out.println(loginResultVO);
        return JsonResult.ok(loginResultVO);
    }

    @PostMapping("logout")
    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 200)
    public JsonResult logout(@AuthenticationPrincipal @ApiIgnore CurrenPrincipal principal) {
        userService.logout(principal);
        return JsonResult.ok();
    }


}
